using System;
using System.Collections;
using TrainGame.Model;


namespace _1830.UI 
{
	public class Global : System.Web.HttpApplication
	{
		static public ArrayList aGames = new ArrayList ();

		static public int getTotalGames ()
		{
			return aGames.Count;
		}

		public static void GetGames (ref string [] games)
		{
			string[] asGames;
			asGames = new string [aGames.Count];

			for (int i = 0; i < aGames.Count; i++)
			{
				Game game = (Game) aGames[i];
				asGames[i] = game.GameName;
			}

			games = asGames;
		}

		static public void addGame (string sGameName, string sPlayerName, string MachineName, string baseFilePath)
		{
			Game newGame = new Game (MachineName, baseFilePath);

			newGame.GameName = sGameName;
			newGame.Creator = sPlayerName;
			aGames.Add (newGame);
		}

		static public bool addPlayer(string sGameName, string sPlayerName, bool isAiPlayer, ref string sError) // returns false if player added.
		{
			foreach (Game game in aGames)
			{
				if (game.GameName == sGameName)
				{
					return game.AddPlayer (sPlayerName, isAiPlayer, ref sError);
				}
			}

			sError = "Error:  Didn't find the game: " + sGameName;
			return true;
		}


        internal static void ReplaceGame(System.Web.UI.Page webPage, Game newGame)
        {
            string sGameName = webPage.Session["GameName"] as string;
            Game  removeGame = null;

            foreach (Game game in aGames)
            {
                if (game.GameName == sGameName)
                {
                    removeGame = game;
                    break;
                }
            }            
            
            if (removeGame != null)
            {
                aGames.Remove (removeGame);
                aGames.Add (newGame);
            }
        }


		static public Game getGame (string sGameName) 
		{
			foreach (Game game in aGames)
			{
				if (game.GameName == sGameName)
				{
					return game;
				}
			}

			return null;
		}

        static public Game getGame(System.Web.UI.Page webPage)  // JWF 10.28.2007
        {
            string sGameName = webPage.Session["GameName"] as string;
            return getGame (sGameName);
        }

 		static public bool doesGameExist (string sGameName)
		{
			bool bReturn = false;

			for (int i = 0; i < aGames.Count; i++)
			{
				Game game = aGames[i] as Game;

				if (game.GameName == sGameName)
				{
					bReturn = true;
					break;
				}
			}

			return bReturn;
	}

		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start(Object sender, EventArgs e)
		{
		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{

		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_Error(Object sender, EventArgs e)
		{

		}

		protected void Session_End(Object sender, EventArgs e)
		{

		}

		protected void Application_End(Object sender, EventArgs e)
		{

		}
			
		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion

        internal static void addGame(Game restoredGame)
        {
            aGames.Add(restoredGame);
        }
    }
}

